/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vzone.market;

import java.util.ArrayList;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import vzone.community.Community;
import vzone.structs.Company;
import vzone.structs.OperatorCode;
import vzone.tools.Date;
import vzone.tools.SqlHelper;

/**
 *
 * @author Kingpro
 */
public class Market implements IMarket
{

    private static final String NEWSHOP = "INSERT INTO [m_shop] VALUES(?,?,?,?,?,?,?)";
    private static final String SELECTSHOP = "SELECT * FROM [m_shop]";

    private static Market market=new Market();

    public static Market getMarket()
    {
	return market;
    }

    public Market()
    {
	
    }
    @Override
    public OperatorCode ApplyForNewShop(Company company)
    {
	company.IdUuid = UUID.randomUUID();
	company.StartD = (new Date()).getTime();
	try
	{
	    SqlHelper.ExecuteNonQuery(SqlHelper.getConnect(), NEWSHOP, new Object[]
		    {
			company.IdUuid.toString(), company.NameS, company.Owner.PeopleIdUuid.toString(), company.StartD, company.accountIdUuid.toString(), company.pwd, company.DescriptionS
		    });
	    return OperatorCode.SUCCESS;
	}
	catch (Exception ex)
	{
	    return OperatorCode.SQLERROR;
	}
    }

    @Override
    public OperatorCode ListAll(ArrayList<Company> lists)
    {
	try
	{
	    //执行SQL
	    ArrayList resultAl = SqlHelper.ExecuteReader(SqlHelper.getConnect(), SELECTSHOP, null);

	    for (int i = 0; i < resultAl.size(); i++)
	    {
		Object[] resultOs = (Object[]) resultAl.get(i);
		Company company = new Company();
		company.IdUuid = UUID.fromString((String) resultOs[0]);
		company.NameS = (String) resultOs[1];
		company.Owner = Community.getCommunity().GetPeopleInfo(UUID.fromString((String) resultOs[2]));

		company.StartD = Long.parseLong(resultOs[3].toString());
		company.accountIdUuid = UUID.fromString((String) resultOs[4]);
		company.pwd = (String) resultOs[5];
		company.DescriptionS = (String) resultOs[6];
		lists.add(company);
	    }
	    return OperatorCode.SUCCESS;
	}
	catch (Exception ex)
	{
	    return OperatorCode.OTHER_ERROR;
	}

    }

    @Override
    public OperatorCode BuyObjects(ArrayList<Company> objsL, UUID BillingID)
    {
	return OperatorCode.NOTSUPPORED;
    }

    @Override
    public Company GetBusinessInfo()
    {
	throw new UnsupportedOperationException("Not supported yet.");
    }
}
